package com.j2eefast.flowable.bpm.controller;

import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.io.file.MimeType;
import com.j2eefast.common.core.utils.*;
import com.j2eefast.flowable.bpm.entity.InformationEntity;
import com.j2eefast.flowable.bpm.service.StudyVidoeService;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.framework.utils.Global;
import com.j2eefast.framework.utils.UserUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
/**
 * <p>知识模块学习视频</p>
 *
 * @author: guoshenghui
 * @date: 2021-05-16 00:02
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Controller
@RequestMapping("/bpm/studyvidoe")
public class StudyVidoeController extends BaseController {
    private String urlPrefix = "modules/bpm/studyvidoe";
    private String typeName = "";
    private String pathName="";
    private Long fileSize=0L;
    @Autowired
    private StudyVidoeService studyVidoeService;


    //学习视频
    @RequiresPermissions("bpm:studyvidoe:studyvidoe")
    @GetMapping("/studyvidoe")
    public String studyvidoe() {
        return urlPrefix + "/studyvidoe";
    }

    //视频列表
    @RequestMapping("/vidoeList")
    @RequiresPermissions("bpm:studyvidoe:vidoeList")
    @ResponseBody
    public ResponseData vidoeList(@RequestParam Map<String, Object> params) {
        PageUtil page = studyVidoeService.VidoeList(params);
        return success(page);
    }
    /**
     * 修改档案页面
     */
    @RequiresPermissions("bpm:studyvidoe:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        InformationEntity informationEntity = studyVidoeService.getVideoById(id);
        mmap.put("information", informationEntity);
        return urlPrefix + "/edit";
    }


    /**
     * 修改认保存
     */
    @RepeatSubmit
    @RequiresPermissions("bpm:studyvidoe:edit")
    @BussinessLog(title = "认领", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData edit(InformationEntity informationEntity) {
        ValidatorUtil.validateEntity(informationEntity);
        return studyVidoeService.updateByIds(informationEntity) ? success() : error("修改失败!");
    }

    /**
     * 删除
     */
    @RepeatSubmit
    @BussinessLog(title = "删除", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @RequiresPermissions("bpm:studyvidoe:del")
    @ResponseBody
    public ResponseData deleteArchive(HttpServletRequest request) {
        String str = request.getParameter("ids");
        if (str != "") {
            String[] s = str.split(",");
            Long[] ids = (Long[]) ConvertUtils.convert(s, Long.class);
            return studyVidoeService.deleteVideo(ids) ? success() : error("删除失败!");
        } else {
            return this.error("删除失败");
        }
    }

    /**
     * 新增参数配置
     */
    @RequiresPermissions("bpm:studyvidoe:add")
    @GetMapping("/add")
    public String add() {
        return urlPrefix + "/add";
    }

    /**
     * 新增保存
     */
    @RepeatSubmit
    @RequiresPermissions("bpm:studyvidoe:add")
    @BussinessLog(title = "保存", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData addSave(@Validated InformationEntity informationEntity) {
        if (pathName != null || pathName != "") {
            if (Arrays.asList(MimeType.MEDIA_EXTENSION).contains(typeName)) {
                informationEntity.setType(0);
            }else{
                return error("上传视频失败！！" );
            }
            informationEntity.setCategory(1);
            Integer userId= Integer.parseInt(UserUtils.getUserInfo().getId().toString());
            String username= UserUtils.getUserInfo().getUsername();
            informationEntity.setUserId(userId);
            informationEntity.setUsername(username);
            informationEntity.setUrl(pathName);
            informationEntity.setSize(fileSize.toString());
            return studyVidoeService.upload(informationEntity) ? success() : error("新增失败!");
        }else {
            return error("上传视频失败！！" );
        }
    }

    /**
     * 上传资料
     */
    @RepeatSubmit
    @BussinessLog(title = "上传视频", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/doUploadFile", method = RequestMethod.POST)
    @RequiresPermissions("bpm:studyvidoe:doUploadFile")
    @ResponseBody
    public ResponseData doUploadFile(MultipartFile file) {
        try {
            if (!file.isEmpty()) {
                pathName = FileUploadUtil.uploadWeb(Global.getTempPath(), file);
                typeName = pathName.substring(pathName.indexOf(".")+1);
                fileSize=file.getSize();
                return success();
            } else {
                return error(ToolUtil.message("sys.file.null"));
            }
        } catch (IOException e) {
            return error("上传视频失败" + e);
        }
    }


}
